Ext.Loader.setConfig({enabled: true});
Ext.Loader.setPath('Ext.ux', 'scripts/ext/ux/');

Ext.require([
    'Ext.tab.*',
    'Ext.ux.extend.*',
    'Ext.window.*',
    'Ext.tip.*',
    'Ext.layout.container.Border'
]);

Ext.onReady(function() {
	Ext.form.Field.prototype.msgTarget = 'qtip';
	Ext.QuickTips.init();
    Ext.form.Field.prototype.msgTarget = 'side';
    var BrowseTxt = Ext.create('Ext.toolbar.TextItem', { text: '最佳浏览器：Chrome,Firefox'});
	var loginForm = new Ext.FormPanel({
		el : 'hello-tabs',
		itemId : 'loginForm',
		name : 'loginForm',
		autoTabs : true,
		activeTab : 0,
		deferredRender : false,
		border : false,
		items :[{
			xtype : 'label',
			height:72,
			html:'<img id="headImg" src="styles/images/login.png" alt="欢迎进入" style="width:100%;paddding:0;magin:0,left:0,top:0"/>'
		},{
			xtype : 'tabpanel',
			height:158,
			activeTab : 0,
			defaults : {autoHeight : true,bodyStyle : 'padding:10 20 30 30'},
			items : [{
				title : '身份认证',
				contentEl : 'loginInfo',
				defaults : {width : 230},
				layout:'anchor',
				defaultType : 'textfield',
				items : [{
					fieldLabel : '用户名',
					name : 'userName',
					value: getCookie('userName'),
					style : 'font-size: 15px',
					width : 300,
					allowBlank : false,
					blankText : '用户名不能为空！',
					enableKeyEvents: true,  
					listeners:{
						'keypress':function(form, e) {
		           			if (e.getKey() == e.ENTER) {  
				                this.nextSibling().focus(); 
				            } 
		        		}
					}
				}, {
					fieldLabel : '密&nbsp;&nbsp;&nbsp;码',
					name : 'userPwd',
					value: getCookie('userPwd'),
					style : 'font-size: 15px',
					inputType : 'password',
					width : 300,
					allowBlank : false,
					blankText : '密码不能为空！',
					enableKeyEvents: true,  
					listeners:{
						'keypress':function(form, e) {
		           			if (e.getKey() == e.ENTER) {  
		           				Ext.getCmp('validateCode').focus();
				            } 
		        		}
					}
				},{
				    xtype: 'container',
				    layout: {
				        type: 'column'
				    },
				    width : 350,
				    items: [{
							xtype: 'textfield',
							fieldLabel : '验证码',
							name : 'validateCode',
							maxLength : 4,
							columnWidth: 0.7,
							style : '',
							allowBlank : false,
							blankText : '验证码不能为空!',
							enableKeyEvents: true,  
							listeners:{
								'keypress':function(form, e) {
				           			if (e.getKey() == e.ENTER) {  
						                doLogin(); 
						            } 
				        		}
							}
						},imgCode = Ext.create('Ext.Img', {
							itemId:'imgCode',
						    src: 'genCodedo.action',
						    style: {
						    	padding:'2 2 2 10',cursor:'pointer'
					        },
							listeners:{
								el: {
						            'click': function() {
						            	imgCode.setSrc('genCodedo.action?'+ Math.random());
						            }
						        }
							}
						})
				    ]
				}]},{
					itemId:'pushInfo',
					title : '系统说明',
					layout : 'fit',
					defaults : {width : 230},
					listeners: {
						'render': function(){
							Ext.Ajax.request({
							    waitMsg:'正在获取，请稍候...',
								waitTitle:'提示',
								url:'sysInfo.action',
								method:'post',
								params:{infoId:1},
							    success: function(response){
							        var jsonObj = Ext.JSON.decode(response.responseText);
							        var opanel = loginForm.down('#pushInfo');
							        opanel.body.update(jsonObj.msg); 
							    },
								failure : function(response) {
									var opanel = loginForm.down('#pushInfo');
							        opanel.body.update("消息获取失败！"); 
								}
							});
						}
					}
				}, {
					itemId:'sysDescription',
					title : '消息公告',
					layout : 'fit',
					defaults : {width : 230},
					listeners: {
						'render': function(){
							Ext.Ajax.request({
							    waitMsg:'正在获取，请稍候...',
								waitTitle:'提示',
								url:'sysInfo.action',
								method:'post',
								params:{infoId:2},
							    success: function(response){
							        var jsonObj = Ext.JSON.decode(response.responseText);
							        var opanel = loginForm.down('#sysDescription');
							        opanel.body.update(jsonObj.msg); 
							    },
								failure : function(response) {
									var opanel = loginForm.down('#sysDescription');
							        opanel.body.update("消息获取失败！"); 
								}
							});
						}
					}
				}]
			}]
	});
	var win = new Ext.Window({
		layout : 'fit',
		width : 460,
		height : 290,
		closeAction : 'hide',
		plain : true,
		collapsible : true,
		maximizable : false,
		draggable : true,
		closable : false,
		resizable : false,
		animateTarget : Ext.getBody(),
		items : loginForm,
        dockedItems: [{
	        xtype: 'toolbar',
	        dock: 'bottom',
	        ui: 'footer',
	        items: [BrowseTxt, '-', getBrowserName(), '-', {
	            itemId: 'chbox_rememberme',
	            xtype: 'checkboxfield',
	            fieldLabel: '',
	            boxLabel: '记住我',
	            checked: true,
	            anchor: '30%'
	        },{
				text : '登录',
				width:70,
				handler : doLogin
			}, {
				text : '取消',
				width:70,
				handler : function() {
					win.hide();
			}}]
	    }]
	}).show();
	
	function doLogin() {
		if (loginForm.form.isValid()) {
			values = loginForm.form.getValues();
			var JSONobj = [];
		    JSONobj.push(JSON.stringify(values));
			loginForm.form.submit({
				url : 'doLogin.action',
				waitTitle : '提示',
				method : 'POST',
				params:{datas:JSONobj},
				waitMsg : '正在验证,请稍候...',
				success : function(form, action) {
					if (win.down('#chbox_rememberme').checked) {
                        SetCookie('userName', loginForm.form.findField('userName').getValue());
                        SetCookie('userPwd', loginForm.form.findField('userPwd').getValue());
                    } else {
                        delCookie('userName');
                        delCookie('userpwd');
                    }

			    var scriptEls = document.getElementsByTagName('script'),
			        path = scriptEls[scriptEls.length - 1].src,
			        theme = getQueryParam('theme',path) || 'neptune',
			        neptune = (theme === 'neptune');
			        
					window.location.href = 'maindo.action' + (neptune?'':'?theme='+ theme);
					mask.hide();
				},
				failure : function(form, action) {
					var errmsg = action.result.msg;
					 Ext.MessageBox.show({
			           title: '错误',
			           msg: errmsg,
			           buttons: Ext.MessageBox.OK,
			           icon: Ext.MessageBox.ERROR,
			           fn: function(id, msg){  
					      loginForm.form.reset();
					    }  
			       });
				}
			});
		}
	}
});
